#include "Transformations.h"
#include "MYModel.h"

Transformations *Transformations::instance = 0;

Transformations::Transformations(){
	nb=MYModel::getInstance()->getSize();
	hc=h_case;
	hb=h_board;
	hw=h_wall;
	sc=t_case;
	ss=t_espacement;
	sb=sc+ss;
	w = nb*sb-ss;
	h =sc*(nb+4)+ss*(nb+3);
}

Transformations* Transformations::getInstance(){
	if(!instance)
		instance = new Transformations();
	return instance;
}

void Transformations::trans_cases(ref_ptr<Camera> camera,ref_ptr<Geode> cases){
	for(int i = 0; i < nb; i++)
		for(int j = 0; j < nb; j++)
		{
			ref_ptr<MatrixTransform> caseTrans = new MatrixTransform;
			caseTrans->addChild(cases);
			caseTrans->setMatrix(Matrix::identity());
			caseTrans->postMult(Matrix::translate(sb*i,sb*j,0));
			camera->addChild(caseTrans);
		}
}

void Transformations::trans_murs(ref_ptr<Camera> camera,ref_ptr<Geode> murs){
	//mur bas MatrixTransform
	MYModel* m = MYModel::getInstance();
	for(int i = 0; i < nb+1; i++)
	{
		MYWall* wallTrans = new MYWall();
		wallTrans->addChild(murs);
		wallTrans->setIndice(i);
		camera->addChild(wallTrans);
		WallListener::getInstance()->add(wallTrans);
	}
	//mur haut MatrixTransform
	for(int i = 0; i < nb+1; i++)
	{ 
		MYWall* wallTrans = new MYWall();
		wallTrans->setIsRouge(true);
		wallTrans->addChild(murs);
		wallTrans->setIndice(i);
		camera->addChild(wallTrans);
		WallListener::getInstance()->add(wallTrans);
	}
	WallListener::getInstance()->traiterEvent();
}

void Transformations::trans_rangements(ref_ptr<Camera> camera, ref_ptr<Geode> rangements){
	//rangement bas MatrixTransform
	for(int i = 0; i < nb; i++)
	{
		ref_ptr<MatrixTransform> rangTrans = new MatrixTransform;
		rangTrans->addChild(rangements);
		rangTrans->setMatrix(Matrix::identity());
		rangTrans->postMult(Matrix::translate(sb*i,-sb,0));
		camera->addChild(rangTrans);
	}
	//rangement haut MatrixTransform
	for(int i = 0; i < nb; i++)
	{
		ref_ptr<MatrixTransform> rangTrans = new MatrixTransform;
		rangTrans->addChild(rangements);
		rangTrans->setMatrix(Matrix::identity());
		rangTrans->postMult(Matrix::translate(sb*i,-sb+((nb+2)*sb),0));
		camera->addChild(rangTrans);
	}
}